'use strict';

/**
 * @ngdoc function
 * @name webApp.controller:ModifyinformationCtrl
 * @description
 * # ModifyinformationCtrl
 * Controller of the webApp
 */

angular.module('webApp')
  .controller('modifyinformation', function ($scope,jqLite,Restangular,cookie,popBox,$location) {
  //序列化
  var param = function(obj) {
    var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
    for(name in obj) {
      value = obj[name];
      if(value instanceof Array) {
        for(i=0; i<value.length; ++i) {
          subValue = value[i];
          fullSubName = name + '[' + i + ']';
          innerObj = {};
          innerObj[fullSubName] = subValue;
          query += param(innerObj) + '&';
        }
      }
      else if(value instanceof Object) {
        for(subName in value) {
          subValue = value[subName];
          fullSubName = name + '[' + subName + ']';
          innerObj = {};
          innerObj[fullSubName] = subValue;
          query += param(innerObj) + '&';
        }
      }
      else if(value !== undefined && value !== null)
        query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
    }
    return query.length ? query.substr(0, query.length - 1) : query;
  };
  //提交修改个人信息
  var user = Restangular.one('v2','user');
  $scope.submit = function(){
    if($scope.description == '填写你的邮箱让更多人能联系到自己'){
      if(cookie.getCookie('access_token')){
        user.customPOST(param(
          {'email': jqLite.$('textBottom')[0].value}),'',{},{'Authorization': cookie.getCookie('access_token'),'Content-Type': 'application/x-www-form-urlencoded'}
        ).then(function(){
            popBox.showConfirm('提示','修改信息成功！');
          $location.path('/root/userData');
          });
      }
    }
    else if($scope.description == '填写你的姓名让更多人认识自己'){
      if(cookie.getCookie('access_token')){
        user.customPOST(param(
          {'display_name': jqLite.$('textBottom')[0].value}),'',{},{'Authorization': cookie.getCookie('access_token'),'Content-Type': 'application/x-www-form-urlencoded'}
        ).then(function(){
            popBox.showConfirm('提示','修改信息成功！');
          $location.path('/root/userData');
          })
      }
    }
    else if($scope.description == '填写你的职位让你的朋友更加了解自己'){
      if(cookie.getCookie('access_token')){
        user.customPOST(param({'company': jqLite.$('textBottom')[0].value}),'',{},{'Authorization': cookie.getCookie('access_token'),'Content-Type': 'application/x-www-form-urlencoded'}).then(function(){
          popBox.showConfirm('提示','修改信息成功！');
          $location.path('/root/userData');
        });
      }
    }
    else{
      if(cookie.getCookie('access_token')){
        user.customPOST(param({'gender': gender}),'',{},{'Authorization': cookie.getCookie('access_token'),'Content-Type': 'application/x-www-form-urlencoded'}).then(function(){
          popBox.showConfirm('提示','修改信息成功！');
          $location.path('/root/userData');
        });
      }
    }
  };
  var gender = '女';
  $scope.serverChange = function(data){
    gender = data;
  };
});
